package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.adapter.carddesk.MyFragmentPagerAdapter;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CustomViewPagerInternal;
import com.wwwarehouse.common.tripartite_widget.viewindicator.CirclePageIndicator;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.customView.AutoClickButton;
import com.wwwarehouse.resource_center.eventbus_event.GoodsEvent;
import com.wwwarehouse.resource_center.eventbus_event.RulesEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/15.
 *         <p/>
 *         维护属性
 */

public class GoodsAttributesProtectFragment extends TheParentFragment implements GoodsAttributeProtectListFragment.CallBackListener{

    CustomViewPagerInternal viewPager;
    CirclePageIndicator pageIndicator;
    ArrayList<Fragment> fragmentList;
    MyFragmentPagerAdapter adapter;
    ArrayList<GoodsAttributesBean.ListBean> tagList = new ArrayList<>();
    AutoClickButton mConfirm ;
    int mNum = 7; //第一页显示的条目数量 ，后面的加一条
    ArrayList<EditText>  editTexts  = new ArrayList<>();
    private String categoryUkid="";
    private String businessId;
//    ArrayList<GoodsAttributesBean.ListBean> listBeen; //拼卡回显的数据
//    ArrayList<GoodsAttributesBean.ListBean> searchBeen; //搜索出来的属性
//    ArrayList<GoodsAttributesBean.ListBean> mSelectList = new ArrayList<>();
    public static final int SELECT_ATTRIBUTE_BYCATEGORY = 1;  //根据类目找属性
    @Override
    protected int getContentId() {
        return R.layout.fragment_goods_attributes_protect;
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof GoodsAttributesProtectFragment){
            mActivity.setTitle(R.string.attribute_protect);
        }
    }

    @Override
    protected void init(View view) {
        EventBus.getDefault().register(this);
        viewPager = findView(view,R.id.task_pager);
        pageIndicator = findView(view,R.id.pager_indicator);
        mConfirm = findView(view,R.id.confirm);
        mConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < editTexts.size(); i++) {
                    tagList.get(i).setAttributeValue(editTexts.get(i).getText().toString());
                }
                //给主页传递的保存信息
                EventBus.getDefault().post(new RulesEvent(GoodsEvent.GOODS_ATTRIBUTE_SELECT_NOTICE,tagList));
                popFragment();
            }
        });
    }

    @Override
    public void requestDatas() {
        try {
            Bundle arguments = getArguments();
            if (null!=arguments){
                categoryUkid = arguments.getString("categoryUkid");
                businessId = arguments.getString("businessId");
                tagList = arguments.getParcelableArrayList("AttributeList"); //拼卡来的数据
//                searchBeen = arguments.getParcelableArrayList("data");
            }
            if (null!=tagList&&tagList.size()>0){
                //不需要请求网络
                fragmentList = new ArrayList<>();
                int count = 0;
                if (tagList.size()<=mNum){
                    count = 0;
                }else {
                    int mSize = tagList.size() - mNum;
                    if (mSize>mNum+1){
                        if ((mSize - mNum-1) % (mNum+1) == 0) {
                            count = (mSize - mNum-1) / (mNum+1)+1;
                        } else {
                            count = (mSize - mNum-1) / (mNum+1) + 2;
                        }
                    }else {
                        count = 1 ;
                    }
                }
                for (int i = 0; i <= count; i++) {
                    GoodsAttributeProtectListFragment categoryFragment = new GoodsAttributeProtectListFragment();
                    categoryFragment.setCallBackListener(this);
                    Bundle bundle = new Bundle();
                    bundle.putInt("pos", i);
                    if (i==0){
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        if (tagList.size()<=mNum){
                            for (int i1 = 0; i1 < tagList.size(); i1++) {
                                list.add(tagList.get(i1));
                            }
                        }else {
                            for (int i1 = 0; i1 < mNum; i1++) {
                                list.add(tagList.get(i1));
                            }
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum);
                    }else if (i!=count){
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < mNum + i * (mNum + 1); i1++) {
                            list.add(tagList.get(i1));
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum+1);
                    }else {
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < tagList.size(); i1++) {
                            list.add(tagList.get(i1));
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum+1);
                    }
                    categoryFragment.setArguments(bundle);
                    fragmentList.add(categoryFragment);
                }

                adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
                viewPager.setAdapter(adapter);
                pageIndicator.setViewPager(viewPager, 0);
            }else {
                if (!TextUtils.isEmpty(categoryUkid)){
                    selectAttributeByCategory();
                }else {
                    fragmentList = new ArrayList<>();
                        GoodsAttributeProtectListFragment categoryFragment = new GoodsAttributeProtectListFragment();
                        categoryFragment.setCallBackListener(this);
                        Bundle bundle = new Bundle();
                        bundle.putInt("pos", 0);
                         ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                         bundle.putParcelableArrayList("data",  list);
                          bundle.putInt("num", mNum);
                        categoryFragment.setArguments(bundle);
                        fragmentList.add(categoryFragment);

                    adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
                    viewPager.setAdapter(adapter);
                    pageIndicator.setViewPager(viewPager, 0);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //根据类目找属性
    private void selectAttributeByCategory() {
        Map<String, Object> map = new HashMap();
        Map<String, Number> query = new HashMap<>();
        map.put("categoryUkid",categoryUkid);
        map.put("businessId",businessId);
        query.put("page",1);
        query.put("size",-1);
        map.put("query",query);
        httpPost(ResourceConstant.SELECT_ATTRIBUTE_BYCATEGORY,map,SELECT_ATTRIBUTE_BYCATEGORY,false,"");
    }

    @Override
    protected void loadDatas() {
//        Bundle mData = getArguments();
//        tagList = mData.getParcelableArrayList("data");
//        fragmentList = new ArrayList<>();
//        int count = 0;
//        if (null!=tagList&&tagList.size()<=mNum){
//            count = 0;
//        }else {
//            int mSize = tagList.size() - mNum;
//            if (mSize>mNum+1){
//                if ((mSize - mNum-1) % (mNum+1) == 0) {
//                    count = (mSize - mNum-1) / (mNum+1)+1;
//                } else {
//                    count = (mSize - mNum-1) / (mNum+1) + 2;
//                }
//            }else {
//                count = 1 ;
//            }
//        }
//        for (int i = 0; i <= count; i++) {
//            GoodsAttributeProtectListFragment categoryFragment = new GoodsAttributeProtectListFragment();
//            categoryFragment.setCallBackListener(this);
//            Bundle bundle = new Bundle();
//            bundle.putInt("pos", i);
//            if (i==0){
//                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
//                if (tagList.size()<=mNum){
//                    for (int i1 = 0; i1 < tagList.size(); i1++) {
//                        list.add(tagList.get(i1));
//                    }
//                }else {
//                    for (int i1 = 0; i1 < mNum; i1++) {
//                        list.add(tagList.get(i1));
//                    }
//                }
//                bundle.putParcelableArrayList("data",  list);
//                bundle.putInt("num", mNum);
//            }else if (i!=count){
//                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
//                for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < mNum + i * (mNum + 1); i1++) {
//                    list.add(tagList.get(i1));
//                }
//                bundle.putParcelableArrayList("data",  list);
//                bundle.putInt("num", mNum+1);
//            }else {
//                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
//                for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < tagList.size(); i1++) {
//                    list.add(tagList.get(i1));
//                }
//                bundle.putParcelableArrayList("data",  list);
//                bundle.putInt("num", mNum+1);
//            }
//            categoryFragment.setArguments(bundle);
//            fragmentList.add(categoryFragment);
//        }
//
//        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
//        viewPager.setAdapter(adapter);
//        pageIndicator.setViewPager(viewPager, 0);
    }
    public void onEventMainThread(GoodsEvent event) {
        switch (event.getCode()){
            case GoodsEvent.GOODS_LABE:
                editTexts.clear();
                mConfirm.removeAllView();
                tagList = (ArrayList<GoodsAttributesBean.ListBean>) event.getData();
                fragmentList = new ArrayList<>();
                int count = 0;
                if (tagList.size()<=mNum){
                    count = 0;
                }else {
                    int mSize = tagList.size() - mNum;
                    if (mSize>mNum+1){
                        if ((mSize - mNum-1) % (mNum+1) == 0) {
                            count = (mSize - mNum-1) / (mNum+1)+1;
                        } else {
                            count = (mSize - mNum-1) / (mNum+1) + 2;
                        }
                    }else {
                        count = 1 ;
                    }
                }
                for (int i = 0; i <= count; i++) {
                    GoodsAttributeProtectListFragment categoryFragment = new GoodsAttributeProtectListFragment();
                    categoryFragment.setCallBackListener(this);
                    Bundle bundle = new Bundle();
                    bundle.putInt("pos", i);
                    if (i==0){
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        if (tagList.size()<=mNum){
                            for (int i1 = 0; i1 < tagList.size(); i1++) {
                                list.add(tagList.get(i1));
                            }
                        }else {
                            for (int i1 = 0; i1 < mNum; i1++) {
                                list.add(tagList.get(i1));
                            }
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum);
                    }else if (i!=count){
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < mNum + i * (mNum + 1); i1++) {
                            list.add(tagList.get(i1));
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum+1);
                    }else {
                        ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                        for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < tagList.size(); i1++) {
                            list.add(tagList.get(i1));
                        }
                        bundle.putParcelableArrayList("data",  list);
                        bundle.putInt("num", mNum+1);
                    }
                    categoryFragment.setArguments(bundle);
                    fragmentList.add(categoryFragment);
                }
                viewPager.removeAllViews();
                adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
                viewPager.setAdapter(adapter);
                pageIndicator.setViewPager(viewPager, 0);

                break;
        }

    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
            switch (what){
                case SELECT_ATTRIBUTE_BYCATEGORY:
                    if (TextUtils.equals("0", commonClass.getCode())){
                        GoodsAttributesBean goodsAttributesBean = JSON.parseObject(commonClass.getData().toString(), GoodsAttributesBean.class);
                        tagList = (ArrayList<GoodsAttributesBean.ListBean>) goodsAttributesBean.getList();
                        for (int i = 0; i < tagList.size(); i++) {
                            tagList.get(i).setSelect(true);
                            tagList.get(i).setFromCategory(true);
                        }
                        fragmentList = new ArrayList<>();
                        int count = 0;
                        if (tagList.size()<=mNum){
                            count = 0;
                        }else {
                            int mSize = tagList.size() - mNum;
                            if (mSize>mNum+1){
                                if ((mSize - mNum-1) % (mNum+1) == 0) {
                                    count = (mSize - mNum-1) / (mNum+1)+1;
                                } else {
                                    count = (mSize - mNum-1) / (mNum+1) + 2;
                                }
                            }else {
                                count = 1 ;
                            }
                        }
                        for (int i = 0; i <= count; i++) {
                            GoodsAttributeProtectListFragment categoryFragment = new GoodsAttributeProtectListFragment();
                            categoryFragment.setCallBackListener(this);
                            Bundle bundle = new Bundle();
                            bundle.putInt("pos", i);
                            if (i==0){
                                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                                if (tagList.size()<=mNum){
                                    for (int i1 = 0; i1 < tagList.size(); i1++) {
                                        list.add(tagList.get(i1));
                                    }
                                }else {
                                    for (int i1 = 0; i1 < mNum; i1++) {
                                        list.add(tagList.get(i1));
                                    }
                                }
                                bundle.putParcelableArrayList("data",  list);
                                bundle.putInt("num", mNum);
                            }else if (i!=count){
                                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                                for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < mNum + i * (mNum + 1); i1++) {
                                    list.add(tagList.get(i1));
                                }
                                bundle.putParcelableArrayList("data",  list);
                                bundle.putInt("num", mNum+1);
                            }else {
                                ArrayList<GoodsAttributesBean.ListBean> list = new ArrayList<>();
                                for (int i1 = mNum + (i - 1) * (mNum + 1); i1 < tagList.size(); i1++) {
                                    list.add(tagList.get(i1));
                                }
                                bundle.putParcelableArrayList("data",  list);
                                bundle.putInt("num", mNum+1);
                            }
                            categoryFragment.setArguments(bundle);
                            fragmentList.add(categoryFragment);
                        }

                        adapter = new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentList);
                        viewPager.setAdapter(adapter);
                        pageIndicator.setViewPager(viewPager, 0);
//                        View viewById = adapter.getItem(0).getView().findViewById(R.id.ll);

                    }
                    break;
            }
    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public void bindEditText(EditText et) {
        editTexts.add(et);
        mConfirm.bindEditText(et);
    }

    @Override
    public void setOnClickListener() {
        for (int i = 0; i < editTexts.size(); i++) {
            tagList.get(i).setAttributeValue(editTexts.get(i).getText().toString());
        }
            //跳转选择属性界面,根据属性名模糊查找属性
           GoodsAttributesSelectParentFragment parentFragment  =  new GoodsAttributesSelectParentFragment();
           Bundle  bundle  = new Bundle();
           bundle.putParcelableArrayList("AttributeList",tagList);
           parentFragment.setArguments(bundle);
           pushFragment(parentFragment);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
